require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['zepto','common/mcommon'],function($,com){
        com.init();
		var pageLogic = new PageLogic($,com);
		pageLogic.init();

	});
});
var PageLogic = function($,com){
	return{
		init:function(){
			var _this = this;
			var vehicleNo = com.utils().urlParam("vehicleNo");
			_this.initPrdType(vehicleNo);
		},
		initNav:function(brandName,modelName,displacementL){
			
			$("#brandName").text(brandName);
			$("#modelName").text(modelName);
			$("#displacementL").text(displacementL);
		},
		initPrdType:function(vehicleNo){
			var _this = this;
			var typeIniter = _this.prdTypeIniter();
			typeIniter.prdType = vehicleNo;
			com.ajax({
				url:"/mgetPartDet.json",
				data:{vehicleNo:vehicleNo},
				success:function(res){
					_this.initNav(
								res.data.carList[0].BRAND_NAME,
								res.data.carList[0].MODEL_NAME,
								res.data.carList[0].DISPLACEMENT_L);
					var AbleProd = res.data.AbleProd;
					var AbleProdSGMD = res.data.AbleProdSGMD;
					var tyrelst = res.data.tyreList;
					typeIniter.init(AbleProd,AbleProdSGMD,tyrelst);
				}
			})
		},
		prdTypeIniter:function(){
			return {
				init:function(AbleProd,AbleProdSGMD,tyrelst){
					var _this = this;
					_this.options = [];
					_this.lis = [];
					_this.doByAbleProd(AbleProd);
					_this.doByTyreLst(tyrelst);
					_this.doByBasicType(AbleProdSGMD);
					_this.genTypes();
					_this.bindEvents();
				},
				doByAbleProd:function(AbleProd){
					var _this = this;
					if(AbleProd && AbleProd.length > 0){
						$(AbleProd).each(function(){
							var objType  = this;
							var tmpType;
							if(_this.options){
								$(_this.options).each(function(idx,type){
									if(type.keytype == objType.ktype){
										tmpType = objType;
									}
								});
							}
							if(!tmpType){
								_this.options.push({keytype:objType.ktype,name:objType.modelName});
								_this.lis.push({keytype:objType.ktype,name:objType.modelName,subs:[]});
							}
						});

						$(AbleProd).each(function(){
							var objType = this;
							$(_this.lis).each(function(idx,obj){
								if(objType.ktype == obj.keytype){
									obj.subs.push(objType);
								}
							});
						});
					}
				},
				doByTyreLst:function(tyrelst){
					var _this = this;
					if(tyrelst && tyrelst.length > 0){
						_this.options.push({keytype:"lt",name:"轮胎"});
						_this.lis.push({keytype:"lt",name:"轮胎"});
					}
				},
				doByBasicType:function(AbleProdSGMD){
					var _this = this;
					_this.options.push({keytype:"hxp",name:"油液及化学品"});
					var subArr = [];
					if(AbleProdSGMD && AbleProdSGMD.length > 0){
						$(AbleProdSGMD).each(function(){
							if(this.valveNum == "10H"){
								subArr.push({valveNum:"10H",modelNo:"自动变速箱油"});
							}else if(this.valveNum == "10D"){
								subArr.push({valveNum:"10D",modelNo:"汽机油"});
							}
							
						});
						subArr.push({valveNum:"10C",modelNo:"防冻液"});
						subArr.push({valveNum:"10E",modelNo:"制动液"});
						subArr.push({valveNum:"10F",modelNo:"转向助力液"});
						subArr.push({valveNum:"10B",modelNo:"齿轮油"});
						subArr.push({valveNum:"10J",modelNo:"冷媒"});
					}else{
						subArr.push({valveNum:"10C",modelNo:"防冻液"});
						subArr.push({valveNum:"10D",modelNo:"汽机油"});
						subArr.push({valveNum:"10E",modelNo:"制动液"});
						subArr.push({valveNum:"10F",modelNo:"转向助力液"});
						subArr.push({valveNum:"10H",modelNo:"自动变速箱油"});
						subArr.push({valveNum:"10B",modelNo:"齿轮油"});
						subArr.push({valveNum:"10J",modelNo:"冷媒"});
					}
					_this.lis.push({keytype:"hxp",name:"油液及化学品",subs:subArr});
				},
				genTypes:function(){
					var _this = this;
					var htmlOption = "<option value='0'>全部</option>";
					$(_this.options).each(function(){
						htmlOption += "<option value='" + this.keytype + "'>" + this.name + "</option>";
					});
					$("#sePrdType").html(htmlOption);
					var html = "";
					$(_this.lis).each(function(){
						var subIco = '';
						if(this.subs && this.subs.length >0){
						   subIco = '<i class="icon"></i> ';
						}
						html += '<li class="item" tg="' + this.keytype + '"> ';
						html += '	<div class="item-top"  par="' + _this.prdType + '" tg="' + this.keytype + '" > ';
						html += '		' + this.name + subIco;
						html += '	</div> ';
						if(this.subs && this.subs.length >0){
							html += '	<ul class="item-subs"> ';
							$(this.subs).each(function(idx,obj){
								html += '<li class="item-sub" par="' + _this.prdType + '" tg="' + obj.valveNum + '"><span>';
								html += obj.modelNo
								html += '</span></li> ';
							});
							html += '	</ul> ';
						}
						
						html += '</li> ';
					});
					$(".types").html(html);
				},
				bindEvents:function(){
					$("#sePrdType").on("change",function(){
						var val = $(this).val();
						if(val == "0"){
							$(".item").removeClass("hide");
						}else{
							$(".item").each(function(){
								var tg = $(this).attr("tg");
								if(tg == val){
									$(this).removeClass("hide");
								}else{
									$(this).addClass("hide");
								}
							})
						}
					});
					$(".types").find("li").find(".item-top").on("click",function(){
						var tg = $(this).attr("tg");
						if(tg == "lt"){
							var url = "partLtCar.htm";
							url += "?vehicleNo=" + $(this).attr("par");
							com.utils().windowJump(url);
							return;
						}
						if($(this).hasClass("active")){
							$(this).removeClass("active");
						}else{
							$(this).addClass("active");
						}
					});
					$(".item-sub").on("click",function(){
						var url = "partsQuery2.htm";
						url += "?vehicleNo=" + $(this).attr("par");
						url += "&sublineNo=" + $(this).attr("tg");
						com.utils().windowJump(url);
					});
				}
			}
		}
	}
}